/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.internal.model.P4Label;

import org.eclipse.core.runtime.MultiStatus;

import java.util.List;
import java.util.Map;

/**
 * ClientUser object for the 'p4 labels' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class Labels extends AbstractPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the results of the operation. */
  private List<P4Label> resultList = Lists.newArrayList();
  /** Stores the Perforce Server. */
  private IPerforceServer server;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for Labels.
   * 
   * @param theServer the perforce server that executes the command.
   */
  public Labels(IPerforceServer theServer) {
    super(new MultiStatus(PerforceCorePlugin.ID, 0, "Error during 'p4 labels'.", null)); //$NON-NLS-1$

    server = theServer;
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void outputStat(Map<String, ?> strDict) {
    P4Label label = new P4Label(server, Helper.safeGetString(strDict, "label")); //$NON-NLS-1$
    label.setLastUpdate(Helper.safeGetDate(strDict, "Update")); //$NON-NLS-1$
    label.setLastAccess(Helper.safeGetDate(strDict, "Access")); //$NON-NLS-1$
    label.setOwner(Helper.safeGetString(strDict, "Owner")); //$NON-NLS-1$
    label.setDescription(Helper.safeGetString(strDict, "Description")); //$NON-NLS-1$

    boolean locked = "locked".equals(Helper.safeGetString(strDict, "Options")); //$NON-NLS-1$
    label.setLocked(locked);

    resultList.add(label);
  }

  /**
   * Called to retrieve the result of the operation.
   * 
   * @return array of labels.
   */
  public P4Label[] getResult() {
    return resultList.toArray(new P4Label[resultList.size()]);
  }

  public String getCommand() {
    return "labels"; //$NON-NLS-1$
  }
}
